<template>
  <div class="wrapper">
    <!-- 抽奖模版 -->
    <!-- <el-card class="form-wrapper" shadow="never">
      <el-form class="form" :model="formData" inline>
        <el-form-item label="标题">
          <el-input v-model="formData.title" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="分类">
          <el-input v-model="formData.cateId" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="formData.status" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card> -->
    <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
      <el-button type="primary" size="small" plain @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>
      <div class="table-box">
        <div class="table-item" v-for="(item, index) in pager.lists">
          <el-card style="max-width: 480px">
            <template #header>
              <div style="display: flex; justify-content: space-between">
                <div class="left">
                  {{ item.title }}
                </div>
                <div class="right">
                  <el-button type="primary" link @click="handleEdit(item)">编辑</el-button>
                </div>
              </div>
            </template>
            <img :src="item.imgHead" style="width: 300px; height: 300px" />
          </el-card>
        </div>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
  </div>
</template>

<script lang="ts" setup>
import { deleteApi, listsApi, statusApi } from '@/api/activity/draw'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { nextTick, onMounted, reactive, ref, shallowRef } from 'vue'
import pagination from '@/components/pagination/index.vue'
const host = import.meta.env.VITE_APP_BASE_URL
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 表单数据
const formData = reactive({
  id: '',
  title: '',
  cateId: '',
  status: '',
})
const showEdit = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: listsApi,
  params: formData,
})

const changeStatus = async (active: any, id: number) => {
  try {
    await feedback.confirm(`确定${active ? '停用' : '开启'}当前管理员？`)
    await statusApi({ id })
    feedback.msgSuccess('修改成功')
    getLists()
  } catch (error) {
    getLists()
  }
}
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await deleteApi({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

const merchants = reactive([])
const loading = ref(false)
const searchMerchants = async () => {
  // loading.value = true
  // const data = await getMerchantListsApi({
  //   name: formData.merchantName
  // })
  // merchants.values = data.lists
  // loading.value = false
}
onMounted(() => {
  getLists()
})
</script>
<style lang="scss" scoped>
.text_hidden {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.wrapper {
  .form-wrapper {
    padding-bottom: 0px;

    .form {
      // margin-bottom: 14px;
    }
  }

  .table-box {
    margin-top: 14px;

    .table-item {
      display: inline-block;
      margin: 10px;
    }
  }
}
</style>
